#include <iostream>
#include <BinaryTree.hpp>

using namespace std;

class Solution
{
    int lastVal = -1;
    int minDiff = 10000;

public:
    int getMinimumDifference(TreeNode *root)
    {
        dfs(root);
        return minDiff;
    }

    void dfs(TreeNode *root)
    {
        if (root->left == nullptr && root->right == nullptr)
        {
            PopVal(root->val);
            return;
        }
        if (root->left != nullptr)
        {
            dfs(root->left);
        }
        PopVal(root->val);
        if (root->right != nullptr)
        {
            dfs(root->right);
        }
    }

    void PopVal(int val)
    {
        if (lastVal == -1)
        {
            lastVal = val;
            return;
        }
        minDiff = min(minDiff, val - lastVal);
        lastVal = val;
    }
};

int main()
{
    cout << "Code530" << endl;
    Solution s;
    TreeNode *root = CreatTree("[4,2,6,1,3]", "null");
    cout << s.getMinimumDifference(root);
    return 0;
}